using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_Scan_Scan : Form
    {
        //定义委托
        public delegate void MyDelegate();
        //定义事件
        public event MyDelegate MyEvent;
        public string A1;
        public Sec_Scan_Scan()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("测试地址不能为空！");
                return;
            }
            else
            {
                string Url = textBox1.Text;
                Sec_Api.Sec_Request_Service_Api.Url = Url;//传出网站域名
                try
                {
                    //这里不知道存在什么BUG 采用接口的方式不行 != null 也是不可以的
                    Sec_Service.Sec_Request_Service.RequestGet(Sec_Api.Sec_Request_Service_Api.A1);
                    //if(Sec_Api.Sec_Request_Service_Api.StatusOk.ToString() == "访问成功！")
                    //{
                    //    MessageBox.Show(Sec_Api.Sec_Request_Service_Api.StatusOk.ToString());
                    //}
                    //else
                    //{
                    //    MessageBox.Show(Sec_Api.Sec_Request_Service_Api.StatusExps.ToString());
                    //}
                    //采用缓存的方法
                    object ok = Sec_Config.Sec_CacheInfo.CacheValue("okinfo");
                    object no = Sec_Config.Sec_CacheInfo.CacheValue("okno");
                    if (ok.ToString().Length > 1)
                    {
                        MessageBox.Show("网站访问成功！");
                    }
                    else
                    {
                        MessageBox.Show("网站访问失败！");
                    }
                    Sec_Config.Sec_CacheInfo.CacheNull("okinfo");
                    Sec_Config.Sec_CacheInfo.CacheNull("okno");
                }
                catch (Exception)
                {
                    MessageBox.Show("网站访问错误！");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "端口信息扫描")
            {
                if (comboBox3.Text == "GET 非SSL请求")
                {
                    MessageBox.Show("在端口信息扫描中不支持GET 非SSL请求，请重新选择！");
                    return;
                }
                if (comboBox3.Text == "GET SSL协议请求")
                {
                    MessageBox.Show("在端口信息扫描中不支持GET SSL协议请求，请重新选择！");
                    return;
                }
                if (comboBox3.Text == "POST 非SSL请求")
                {
                    MessageBox.Show("在端口信息扫描中不支持POST 非SSL请求，请重新选择！");
                    return;
                }
                if (comboBox3.Text == "POST SSL协议请求")
                {
                    MessageBox.Show("在端口信息扫描中不支持POST SSL协议请求，请重新选择！");
                    return;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(textBox1.Text))
                {
                    MessageBox.Show("扫描地址不能为空！");
                    return;
                }
                else
                {
                    //获取扫描地址后进行传出
                    string url = textBox1.Text;
                    if(string.IsNullOrEmpty(url) || string.IsNullOrEmpty(comboBox2.Text) || string.IsNullOrEmpty(comboBox1.Text) || string.IsNullOrEmpty(comboBox3.Text))
                    {
                        MessageBox.Show("数据不能为空！");
                        return;
                    }
                    Sec_Api.Sec_LoopScan_Service_Api.Url = url;//漏洞扫描地址
                    Sec_Api.Sec_LoopScan_Service_Api.ScanType = comboBox1.Text;//扫描类型
                    Sec_Api.Sec_LoopScan_Service_Api.RequesType = comboBox3.Text;//请求方式
                    Sec_Api.Sec_LoopScan_Service_Api.ScanProxy = comboBox2.Text;//扫描代理
                    //Sec_Service.Sec_LoopScan_Service.LoopScanInfo(Sec_Api.Sec_LoopScan_Service_Api.P1);//执行漏洞扫描
                    //保存扫描信息

                    //执行完成后 执行关闭事件
                    if (MyEvent != null)
                        MyEvent();
                    this.Close();
                }
            }
        }
        /// <summary>
        /// 执行选择 根据不同的选择执行对应的扫描及信息探测
        /// </summary>
        public void StartScanSelect()
        {
            if(comboBox1.Text == "端口信息扫描")
            {
                if(comboBox3.Text == "GET 非SSL请求")
                {
                    MessageBox.Show("不支持GET 非SSL请求，请重新选择！");
                    return;
                }
                if (comboBox3.Text == "GET SSL协议请求")
                {
                    MessageBox.Show("不支持GET SSL协议请求，请重新选择！");
                    return;
                }
                if (comboBox3.Text == "POST 非SSL请求")
                {
                    MessageBox.Show("不支持POST 非SSL请求，请重新选择！");
                    return;
                }
                if (comboBox3.Text == "POST SSL协议请求")
                {
                    MessageBox.Show("不支持POST SSL协议请求，请重新选择！");
                    return;
                }
            }
            //直接封装一个类 通过类来读取选择项
            Sec_Api.Sec_ScanSelect_Config_Api.Url = textBox1.Text;//URL地址传出
            Sec_Api.Sec_ScanSelect_Config_Api.ScanType = comboBox1.Text;//扫描类型选择
            Sec_Api.Sec_ScanSelect_Config_Api.RequestType = comboBox3.Text;//请求方式传出
            Sec_Api.Sec_ScanSelect_Config_Api.ProxxSelect = comboBox2.Text;//代理扫描传出
            Sec_System.Sec_ScanSelect_Config.ScanSelectInfo(Sec_Api.Sec_ScanSelect_Config_Api.S1);//执行后进行判断
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }
        public int oldX, oldY;
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //判断是否为鼠标左键
            if (e.Button == MouseButtons.Left)
            {
                //获取鼠标左键按下时的位置
                this.oldX = e.Location.X;
                this.oldY = e.Location.Y;
            }
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //计算鼠标移动距离
                this.Left += e.Location.X - this.oldX;
                this.Top += e.Location.Y - this.oldY;
            }
        }
    }
}
